#!/bin/sh
user="201631062127"
passwd="123659"
dir="/root/login"
url="http://172.16.245.50/include/auth_action.php"
dir="login"

if [ ! -d "$dir" ]; then
        mkdir $dir
fi

login(){
#get ip
ip=`ifconfig | grep 'inet addr'| grep -vE '(192|127)' | awk '{print $2}' | awk -F ':' '{print $ 2}'`
echo $ip
#system log
logger "edu login:" "ip: $ip ,user: $user, password: $passwd, login now"

#login

wget -qP $dir --post-data="action=login&username=$user@yd&password=$passwd&ac_id=2&user_ip=$ip&nas_ip=&user_mac=&save_me=1&ajax=1" $url

if [ $? != 0 ]; then 
	logger "edu login:" "can not get the page!"
	sh /root/login.sh&
	exit
	#echo "error"
fi
}

logger "edu login:" "wait 15 seconds"
sleep 15

#start login
login

#
res=`cat $dir/auth_action.php | awk -F ',' '{print $1}'`
#echo $res
rm -f $dir/auth_action.php
if [ "$res" == "login_ok" ]; then
	logger "edu login:" "$res"
	echo $res
	#/root/ssmtp.sh "edu status" "ip: $ip\nuser: $user\nstatus: online" "1399092738@qq.com"
	#/root/ssmtp.sh "edu status" "$user online" "eddy_s@qq.com"
	#logger "edu login:" "protection of edu is starting"
	val=`ps | grep "protect.sh" | grep -v "grep"`
	if [ "$val" == "" ]; then
        	/root/protect.sh &
	fi	
	#/root/protect.sh &
elif [ "$res" == "E2620: You are already online.(已经在线了)" ]; then
	echo $res
	logger "edu login:" "$res,start logout"
	sleep 5
	/root/logout.sh
fi
